$(document).ready(function() {		
    $('.brokers').addClass('active');
    //$('#txt_ASP_RefID').bestupper();
    //$('#txt_ASP_RefID').alphanumeric();
    //$('#txt_ASP_RefID').textLimit(10);
	$('#txt_ASP_RefID').val().toUpperCase();
    $('#txt_ASP_FirstName').textLimit(50);
    $('#txt_ASP_Surname').textLimit(50);
    $('#txt_ASP_Telephone_Office').textLimit(20);
    $('#txt_ASP_Telephone_Mobile').textLimit(20);
    $('#txt_ASP_EmailAddress').textLimit(80);
    $('#txt_ASP_WebSiteURL').textLimit(100);
	
	$("#txt_ASP_Surname").blur(function (){
		if ($("#txt_ASP_Surname").val().length > 0 && $("#txt_ASP_FirstName").val().length > 0) {
			$.ajax({					
                type: "GET",
				data: "firstname="+$.trim($("#txt_ASP_FirstName").val())+"&surname="+$.trim($("#txt_ASP_Surname").val()),
                url: "check_name.php",
                success: function(response) {	
                	var firstname = $.trim($("#txt_ASP_FirstName").val().toUpperCase());
                	firstname = firstname.replace(/ |-/g, '');	
                	var surname = $.trim($("#txt_ASP_Surname").val().toUpperCase());
                	surname = surname.replace(/ |-/g, '');	
                	var ref_id = firstname+surname;
                	if (ref_id.length > 10) {
                		ref_id = ref_id.substr(0,10);
                	} 
                    if(response=="NOT")
                    {
                    	if ($("#txtASP_ID").val() != "" && firstname == $("#txtfirstname").val().toUpperCase() && surname == $("#txtsurname").val().toUpperCase()) {
                    		$("#isnamevalid").html("");
                    		$('#txt_ASP_RefID').val(ref_id);
                    		$("#my_image").show();
	                        $("#my_image").attr("src","http://qrcode.kaywa.com/img.php?s=8&d="+$("#txt_ASP_RefID").val());
	                        $("#my_image_link").attr("href", "http://qrcode.kaywa.com/img.php?s=8&d="+$("#txt_ASP_RefID").val());
                    	} else {
		                    $("#isnamevalid").html("<p style='color: red'>Sorry, this name already exists. Please enter another!</p>");	
		                    $("#my_image").hide();	
		                    $('#txt_ASP_RefID').val('');
		                    $("#txtrefid").val("0");
		                }
                    }
                    else
                    {	
                        $("#isnamevalid").html("");
                        $('#txt_ASP_RefID').val(ref_id);
                        $("#my_image").show();
	                    $("#my_image").attr("src","http://qrcode.kaywa.com/img.php?s=8&d="+$("#txt_ASP_RefID").val());
	                    $("#my_image_link").attr("href", "http://qrcode.kaywa.com/img.php?s=8&d="+$("#txt_ASP_RefID").val());
                    }
                }					
            });
		}
	});
	
	$("#txt_ASP_FirstName").blur(function (){
		if ($("#txt_ASP_Surname").val().length > 0 && $("#txt_ASP_FirstName").val().length > 0) {
			$.ajax({					
                type: "GET",
                data: "firstname="+$.trim($("#txt_ASP_FirstName").val())+"&surname="+$.trim($("#txt_ASP_Surname").val()),
                url: "check_name.php",
                success: function(response) {
                	var firstname = $.trim($("#txt_ASP_FirstName").val().toUpperCase());
                	firstname = firstname.replace(/ |-/g, '');	
                	var surname = $.trim($("#txt_ASP_Surname").val().toUpperCase());
                	surname = surname.replace(/ |-/g, '');	
                	var ref_id = firstname+surname;
                	if (ref_id.length > 10) {
                		ref_id = ref_id.substr(0,10);
                	} 
                    if(response=="NOT")
                    {
                    	if ($("#txtASP_ID").val() != "" && firstname == $("#txtfirstname").val().toUpperCase() && surname == $("#txtsurname").val().toUpperCase()) {
                    		$("#isnamevalid").html("");
                    		$('#txt_ASP_RefID').val(ref_id);
                    		$("#my_image").show();
	                        $("#my_image").attr("src","http://qrcode.kaywa.com/img.php?s=8&d="+$("#txt_ASP_RefID").val());
	                        $("#my_image_link").attr("href", "http://qrcode.kaywa.com/img.php?s=8&d="+$("#txt_ASP_RefID").val());
                    	} else {
		                    $("#isnamevalid").html("<p style='color: red'>Sorry, this name already exists. Please enter another!</p>");	
		                    $("#my_image").hide();	
							$('#txt_ASP_RefID').val('');
		                    $("#txtrefid").val("0");
		                }
                    }
                    else
                    {	
                        $("#isnamevalid").html("");
                        $('#txt_ASP_RefID').val(ref_id);
                        $("#my_image").show();
	                    $("#my_image").attr("src","http://qrcode.kaywa.com/img.php?s=8&d="+$("#txt_ASP_RefID").val());
	                    $("#my_image_link").attr("href", "http://qrcode.kaywa.com/img.php?s=8&d="+$("#txt_ASP_RefID").val());
                    }
                }					
            });
		}
	});
	
    /*$("#txt_ASP_RefID").change(function (){
	
        //if($("#txtASP_ID").val()!="" )//&& ($("#txtrefid").val()==" " || $("#txtrefid").val()=="0") )
        //    $("#txtrefid").setValue($("#txt_ASP_RefID").val());
		
        //if($(this).val()!=$("#txtrefid").val())
        //{
            $.ajax({					
                type: "GET",
                data: "refid="+$(this).val(),
                url: "check_refid.php",
                success: function(response) {		
                    if(response=="NOT")
                    {
                    	if ($("#txtASP_ID").val() != "" && $("#txt_ASP_RefID").val() == $("#txtrefid").val()) {
                    		$("#isrefidvalid").html("");
	                        $("#my_image").show();
	                        $("#my_image").attr("src","http://qrcode.kaywa.com/img.php?s=8&d="+$("#txt_ASP_RefID").val());
	                        $("#my_image_link").attr("href", "http://qrcode.kaywa.com/img.php?s=8&d="+$("#txt_ASP_RefID").val());
                    	} else {
		                    $("#isrefidvalid").html("<p style='color: red'>Sorry, this Ref ID already exists. Please enter another!</p>");
		                    //$("#txtrefid").val("");		
		                    $("#my_image").hide();	
		                    $("#txtrefid").val("0");
		                }
                    }
                    else
                    {	
                        $("#isrefidvalid").html("");
                        //$("#txtrefid").val($("#txt_ASP_RefID").val());
                        $("#my_image").show();
                        $("#my_image").attr("src","http://qrcode.kaywa.com/img.php?s=8&d="+$("#txt_ASP_RefID").val());
                        $("#my_image_link").attr("href", "http://qrcode.kaywa.com/img.php?s=8&d="+$("#txt_ASP_RefID").val());
                    }
                }					
            });
        //}
        //else if($(this).attr('value')==$("#txtrefid").val())
        //{
        //    $("#my_image").attr("src","http://qrcode.kaywa.com/img.php?s=8&d="+$("#txt_ASP_RefID").val());
        //    $("#my_image_link").attr("href", "http://qrcode.kaywa.com/img.php?s=8&d="+$("#txt_ASP_RefID").val());
        //    $("#my_image").show();
        //}
    });*/
    
    $('#delete_broker_img').click(function(){
		var answer = confirm ("Are you sure you want to delete this file?");
		if (answer){
			$.post("delete_broker_img.php", "id="+$('#ASP_ID').val());
			$(this).parent().remove();
		}
	});
	
	$('#delete_broker_img_logo').click(function(){
		var answer = confirm ("Are you sure you want to delete this file?");
		if (answer){
			$.post("delete_broker_img_logo.php", "id="+$('#ASP_ID').val());
			$(this).parent().remove();
		}
	});
    
});

/*
 * Various functions
 */
function validate_sales_people()
{
    if(document.getElementById('MM_insert').value=='formSalesPeople')
    {
        var msg='';
        if(document.getElementById('txt_ASP_FirstName').value=='')msg+='Please enter a First Name (mandatory field)';
        else if(document.getElementById('txt_ASP_Surname').value=='')msg+='Please enter Surname (mandatory field)';
        else if(document.getElementById('txt_ASP_EmailAddress').value=='')msg+='Please enter an Email Address (mandatory field)';
        else if(document.getElementById('txt_ASP_Telephone_Office').value=='')msg+='Please enter an Office Phone (mandatory field)';
        else if(document.getElementById('txt_ASP_Telephone_Mobile').value=='')msg+='Please enter a Mobile Phone (mandatory field)';
        else if(document.getElementById('txt_ASP_CompanyName').value=='')msg+='Please enter a Company Name (mandatory field)';
        else if(document.getElementById('txt_ASP_Address1').value=='')msg+='Please enter Broker Office - Address 1 (mandatory field)';
        //else if(document.getElementById('txt_ASP_Address2').value=='')msg+='Please enter Broker Office - Address 1 (mandatory field)';
        //else if(document.getElementById('txt_ASP_Town').value=='')msg+='Please enter Broker Office - Town (mandatory field)';
        else if(document.getElementById('txt_ASP_State').value=='')msg+='Please enter Broker Office - State (mandatory field)';
        else if(document.getElementById('txt_ASP_PostCode').value=='')msg+='Please enter Broker Office - Zip Code (mandatory field)'; 
        //else if(document.getElementById('txt_ASP_RefID').value=='')	msg+='Please enter a Ref Id (mandatory field)';
			
			
        if( msg != '') {
            alert(msg);
            return false;
        } else if ($('#isrefidvalid').html() != '') {
        	return false;
        } else if ($('#isnamevalid').html() != '') {
        	return false;
        } else {
            return true 
        };
    }
}

//v3.0
function MM_jumpMenu(targ,selObj,restore){
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");  
    if (restore) {
        selObj.selectedIndex=0;
    }
  
}
